home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / pyshared / PIL / ImageFont.py < prev    next >
Text File  |  2006-12-03  |  17KB  |  378 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: ImageFont.py 2813 2006-10-07 10:11:35Z fredrik $
  4. #
  5. # PIL raster font management
  6. #
  7. # History:
  8. # 1996-08-07 fl   created (experimental)
  9. # 1997-08-25 fl   minor adjustments to handle fonts from pilfont 0.3
  10. # 1999-02-06 fl   rewrote most font management stuff in C
  11. # 1999-03-17 fl   take pth files into account in load_path (from Richard Jones)
  12. # 2001-02-17 fl   added freetype support
  13. # 2001-05-09 fl   added TransposedFont wrapper class
  14. # 2002-03-04 fl   make sure we have a "L" or "1" font
  15. # 2002-12-04 fl   skip non-directory entries in the system path
  16. # 2003-04-29 fl   add embedded default font
  17. # 2003-09-27 fl   added support for truetype charmap encodings
  18. #
  19. # Todo:
  20. # Adapt to PILFONT2 format (16-bit fonts, compressed, single file)
  21. #
  22. # Copyright (c) 1997-2003 by Secret Labs AB
  23. # Copyright (c) 1996-2003 by Fredrik Lundh
  24. #
  25. # See the README file for information on usage and redistribution.
  26. #
  27.  
  28. import Image
  29. import os, string, sys
  30.  
  31. # FIXME: add support for pilfont2 format (see FontFile.py)
  32.  
  33. # --------------------------------------------------------------------
  34. # Font metrics format:
  35. #       "PILfont" LF
  36. #       fontdescriptor LF
  37. #       (optional) key=value... LF
  38. #       "DATA" LF
  39. #       binary data: 256*10*2 bytes (dx, dy, dstbox, srcbox)
  40. #
  41. # To place a character, cut out srcbox and paste at dstbox,
  42. # relative to the character position.  Then move the character
  43. # position according to dx, dy.
  44. # --------------------------------------------------------------------
  45.  
  46. ##
  47. # The <b>ImageFont</b> module defines a class with the same name.
  48. # Instances of this class store bitmap fonts, and are used with the
  49. # <b>text</b> method of the <b>ImageDraw</b> class.
  50. # <p>
  51. # PIL uses it's own font file format to store bitmap fonts. You can
  52. # use the <b>pilfont</b> utility to convert BDF and PCF font
  53. # descriptors (X window font formats) to this format.
  54. # <p>
  55. # Starting with version 1.1.4, PIL can be configured to support
  56. # TrueType and OpenType fonts.  For earlier version, TrueType
  57. # support is only available as part of the imToolkit package
  58. #
  59. # @see ImageDraw#ImageDraw.text
  60. # @see pilfont
  61.  
  62. class ImageFont:
  63.     "PIL font wrapper"
  64.  
  65.     def _load_pilfont(self, filename):
  66.  
  67.         file = open(filename, "rb")
  68.  
  69.         for ext in (".png", ".gif", ".pbm"):
  70.             try:
  71.                 fullname = os.path.splitext(filename)[0] + ext
  72.                 image = Image.open(fullname)
  73.             except:
  74.                 pass
  75.             else:
  76.                 if image and image.mode in ("1", "L"):
  77.                     break
  78.         else:
  79.             raise IOError("cannot find glyph data file")
  80.  
  81.         self.file = fullname
  82.  
  83.         return self._load_pilfont_data(file, image)
  84.  
  85.     def _load_pilfont_data(self, file, image):
  86.  
  87.         # read PILfont header
  88.         if file.readline() != "PILfont\n":
  89.             raise SyntaxError("Not a PILfont file")
  90.         d = string.split(file.readline(), ";")
  91.         self.info = [] # FIXME: should be a dictionary
  92.         s = file.readline()
  93.         while s and s != "DATA\n":
  94.             self.info.append(s)
  95.  
  96.         # read PILfont metrics
  97.         data = file.read(256*20)
  98.  
  99.         # check image
  100.         if image.mode not in ("1", "L"):
  101.             raise TypeError("invalid font image mode")
  102.  
  103.         image.load()
  104.  
  105.         self.font = Image.core.font(image.im, data)
  106.  
  107.         # delegate critical operations to internal type
  108.         self.getsize = self.font.getsize
  109.         self.getmask = self.font.getmask
  110.  
  111. ##
  112. # Wrapper for FreeType fonts.  Application code should use the
  113. # <b>truetype</b> factory function to create font objects.
  114.  
  115. class FreeTypeFont:
  116.     "FreeType font wrapper (requires _imagingft service)"
  117.  
  118.     def __init__(self, file, size, index=0, encoding=""):
  119.         # FIXME: use service provider instead
  120.         import _imagingft
  121.         self.font = _imagingft.getfont(file, size, index, encoding)
  122.  
  123.     def getname(self):
  124.         return self.font.family, self.font.style
  125.  
  126.     def getmetrics(self):
  127.         return self.font.ascent, self.font.descent
  128.  
  129.     def getsize(self, text):
  130.         return self.font.getsize(text)[0]
  131.  
  132.     def getmask(self, text, mode=""):
  133.         return self.getmask2(text, mode)[0]
  134.  
  135.     def getmask2(self, text, mode="", fill=Image.core.fill):
  136.         size, offset = self.font.getsize(text)
  137.         im = fill("L", size, 0)
  138.         self.font.render(text, im.id, mode=="1")
  139.         return im, offset
  140.  
  141. ##
  142. # Wrapper that creates a transposed font from any existing font
  143. # object.
  144. #
  145. # @param font A font object.
  146. # @param orientation An optional orientation.  If given, this should
  147. #     be one of Image.FLIP_LEFT_RIGHT, Image.FLIP_TOP_BOTTOM,
  148. #     Image.ROTATE_90, Image.ROTATE_180, or Image.ROTATE_270.
  149.  
  150. class TransposedFont:
  151.     "Wrapper for writing rotated or mirrored text"
  152.  
  153.     def __init__(self, font, orientation=None):
  154.         self.font = font
  155.         self.orientation = orientation # any 'transpose' argument, or None
  156.  
  157.     def getsize(self, text):
  158.         w, h = self.font.getsize(text)
  159.         if self.orientation in (Image.ROTATE_90, Image.ROTATE_270):
  160.             return h, w
  161.         return w, h
  162.  
  163.     def getmask(self, text, mode=""):
  164.         im = self.font.getmask(text, mode)
  165.         if self.orientation is not None:
  166.             return im.transpose(self.orientation)
  167.         return im
  168.  
  169. ##
  170. # Load font file.  This function loads a font object from the given
  171. # bitmap font file, and returns the corresponding font object.
  172. #
  173. # @param filename Name of font file.
  174. # @return A font object.
  175. # @exception IOError If the file could not be read.
  176.  
  177. def load(filename):
  178.     "Load a font file."
  179.     f = ImageFont()
  180.     f._load_pilfont(filename)
  181.     return f
  182.  
  183. ##
  184. # Load a TrueType or OpenType font file, and create a font object.
  185. # This function loads a font object from the given file, and creates
  186. # a font object for a font of the given size.
  187. # <p>
  188. # This function requires the _imagingft service.
  189. #
  190. # @param filename A truetype font file.  Under Windows, if the file
  191. #    is not found in this filename, the loader also looks in Windows
  192. #    <b>fonts</b> directory
  193. # @param size The requested size, in points.
  194. # @param index Which font face to load (default is first available face).
  195. # @param encoding Which font encoding to use (default is Unicode).  Common
  196. #    encodings are "unic" (Unicode), "symb" (Microsoft Symbol), "ADOB"
  197. #    (Adobe Standard), "ADBE" (Adobe Expert), and "armn" (Apple Roman).
  198. #    See the FreeType documentation for more information.
  199. # @return A font object.
  200. # @exception IOError If the file could not be read.
  201.  
  202. def truetype(filename, size, index=0, encoding=""):
  203.     "Load a truetype font file."
  204.     try:
  205.         return FreeTypeFont(filename, size, index, encoding)
  206.     except IOError:
  207.         if sys.platform == "win32":
  208.             # check the windows font repository
  209.             # NOTE: must use uppercase WINDIR, to work around bugs in
  210.             # 1.5.2's os.environ.get()
  211.             windir = os.environ.get("WINDIR")
  212.             if windir:
  213.                 filename = os.path.join(windir, "fonts", filename)
  214.                 return FreeTypeFont(filename, size, index, encoding)
  215.         raise
  216.  
  217. ##
  218. # Load font file.  Same as load, but searches for a bitmap font along
  219. # the Python path.
  220. #
  221. # @param filename Name of font file.
  222. # @return A font object.
  223. # @exception IOError If the file could not be read.
  224. # @see #load
  225.  
  226. def load_path(filename):
  227.     "Load a font file, searching along the Python path."
  228.     for dir in sys.path:
  229.         if Image.isDirectory(dir):
  230.             try:
  231.                 return load(os.path.join(dir, filename))
  232.             except IOError:
  233.                 pass
  234.     raise IOError("cannot find font file")
  235.  
  236. ##
  237. # Load a (probably rather ugly) default font.
  238. #
  239. # @return A font object.
  240.  
  241. def load_default():
  242.     "Load a default font."
  243.     from StringIO import StringIO
  244.     import base64
  245.     f = ImageFont()
  246.     f._load_pilfont_data(
  247.          # courB08
  248.          StringIO(base64.decodestring('''
  249. UElMZm9udAo7Ozs7OzsxMDsKREFUQQoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  250. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AB
  264. ACEAAAAkAAgABgAAAAD/+QAE//0AJAAAACgABAAGAAAAAP/6AAX//wAoAAAALQAFAAYAAAAB//8A
  265. BAACAC0AAAAwAAMABgAAAAD//AAF//0AMAAAADUAAQAGAAAAAf//AAMAAAA1AAAANwABAAYAAAAB
  266. //kABQABADcAAAA7AAgABgAAAAD/+QAFAAAAOwAAAEAABwAGAAAAAP/5AAYAAABAAAAARgAHAAYA
  267. AAAA//kABQAAAEYAAABLAAcABgAAAAD/+QAFAAAASwAAAFAABwAGAAAAAP/5AAYAAABQAAAAVgAH
  268. AAYAAAAA//kABQAAAFYAAABbAAcABgAAAAD/+QAFAAAAWwAAAGAABwAGAAAAAP/5AAUAAABgAAAA
  269. ZQAHAAYAAAAA//kABQAAAGUAAABqAAcABgAAAAD/+QAFAAAAagAAAG8ABwAGAAAAAf/8AAMAAABv
  270. AAAAcQAEAAYAAAAA//wAAwACAHEAAAB0AAYABgAAAAD/+gAE//8AdAAAAHgABQAGAAAAAP/7AAT/
  271. /gB4AAAAfAADAAYAAAAB//oABf//AHwAAACAAAUABgAAAAD/+gAFAAAAgAAAAIUABgAGAAAAAP/5
  272. AAYAAQCFAAAAiwAIAAYAAP////oABgAAAIsAAACSAAYABgAA////+gAFAAAAkgAAAJgABgAGAAAA
  273. AP/6AAUAAACYAAAAnQAGAAYAAP////oABQAAAJ0AAACjAAYABgAA////+gAFAAAAowAAAKkABgAG
  274. AAD////6AAUAAACpAAAArwAGAAYAAAAA//oABQAAAK8AAAC0AAYABgAA////+gAGAAAAtAAAALsA
  275. BgAGAAAAAP/6AAQAAAC7AAAAvwAGAAYAAP////oABQAAAL8AAADFAAYABgAA////+gAGAAAAxQAA
  276. AMwABgAGAAD////6AAUAAADMAAAA0gAGAAYAAP////oABQAAANIAAADYAAYABgAA////+gAGAAAA
  277. 2AAAAN8ABgAGAAAAAP/6AAUAAADfAAAA5AAGAAYAAP////oABQAAAOQAAADqAAYABgAAAAD/+gAF
  278. AAEA6gAAAO8ABwAGAAD////6AAYAAADvAAAA9gAGAAYAAAAA//oABQAAAPYAAAD7AAYABgAA////
  279. +gAFAAAA+wAAAQEABgAGAAD////6AAYAAAEBAAABCAAGAAYAAP////oABgAAAQgAAAEPAAYABgAA
  280. ////+gAGAAABDwAAARYABgAGAAAAAP/6AAYAAAEWAAABHAAGAAYAAP////oABgAAARwAAAEjAAYA
  281. BgAAAAD/+gAFAAABIwAAASgABgAGAAAAAf/5AAQAAQEoAAABKwAIAAYAAAAA//kABAABASsAAAEv
  282. AAgABgAAAAH/+QAEAAEBLwAAATIACAAGAAAAAP/5AAX//AEyAAABNwADAAYAAAAAAAEABgACATcA
  283. AAE9AAEABgAAAAH/+QAE//wBPQAAAUAAAwAGAAAAAP/7AAYAAAFAAAABRgAFAAYAAP////kABQAA
  284. AUYAAAFMAAcABgAAAAD/+wAFAAABTAAAAVEABQAGAAAAAP/5AAYAAAFRAAABVwAHAAYAAAAA//sA
  285. BQAAAVcAAAFcAAUABgAAAAD/+QAFAAABXAAAAWEABwAGAAAAAP/7AAYAAgFhAAABZwAHAAYAAP//
  286. //kABQAAAWcAAAFtAAcABgAAAAD/+QAGAAABbQAAAXMABwAGAAAAAP/5AAQAAgFzAAABdwAJAAYA
  287. AP////kABgAAAXcAAAF+AAcABgAAAAD/+QAGAAABfgAAAYQABwAGAAD////7AAUAAAGEAAABigAF
  288. AAYAAP////sABQAAAYoAAAGQAAUABgAAAAD/+wAFAAABkAAAAZUABQAGAAD////7AAUAAgGVAAAB
  289. mwAHAAYAAAAA//sABgACAZsAAAGhAAcABgAAAAD/+wAGAAABoQAAAacABQAGAAAAAP/7AAYAAAGn
  290. AAABrQAFAAYAAAAA//kABgAAAa0AAAGzAAcABgAA////+wAGAAABswAAAboABQAGAAD////7AAUA
  291. AAG6AAABwAAFAAYAAP////sABgAAAcAAAAHHAAUABgAAAAD/+wAGAAABxwAAAc0ABQAGAAD////7
  292. AAYAAgHNAAAB1AAHAAYAAAAA//sABQAAAdQAAAHZAAUAB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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAB
  306. //sAAwACAecAAAHpAAcABgAAAAD/+QAFAAEB6QAAAe4ACAAGAAAAAP/5AAYAAAHuAAAB9AAHAAYA
  307. AAAA//oABf//AfQAAAH5AAUABgAAAAD/+QAGAAAB+QAAAf8ABwAGAAAAAv/5AAMAAgH/AAACAAAJ
  308. AAYAAAAA//kABQABAgAAAAIFAAgABgAAAAH/+gAE//sCBQAAAggAAQAGAAAAAP/5AAYAAAIIAAAC
  309. DgAHAAYAAAAB//kABf/+Ag4AAAISAAUABgAA////+wAGAAACEgAAAhkABQAGAAAAAP/7AAX//gIZ
  310. AAACHgADAAYAAAAA//wABf/9Ah4AAAIjAAEABgAAAAD/+QAHAAACIwAAAioABwAGAAAAAP/6AAT/
  311. +wIqAAACLgABAAYAAAAA//kABP/8Ai4AAAIyAAMABgAAAAD/+gAFAAACMgAAAjcABgAGAAAAAf/5
  312. AAT//QI3AAACOgAEAAYAAAAB//kABP/9AjoAAAI9AAQABgAAAAL/+QAE//sCPQAAAj8AAgAGAAD/
  313. ///7AAYAAgI/AAACRgAHAAYAAAAA//kABgABAkYAAAJMAAgABgAAAAH//AAD//0CTAAAAk4AAQAG
  314. AAAAAf//AAQAAgJOAAACUQADAAYAAAAB//kABP/9AlEAAAJUAAQABgAAAAH/+QAF//4CVAAAAlgA
  315. BQAGAAD////7AAYAAAJYAAACXwAFAAYAAP////kABgAAAl8AAAJmAAcABgAA////+QAGAAACZgAA
  316. Am0ABwAGAAD////5AAYAAAJtAAACdAAHAAYAAAAA//sABQACAnQAAAJ5AAcABgAA////9wAGAAAC
  317. eQAAAoAACQAGAAD////3AAYAAAKAAAAChwAJAAYAAP////cABgAAAocAAAKOAAkABgAA////9wAG
  318. AAACjgAAApUACQAGAAD////4AAYAAAKVAAACnAAIAAYAAP////cABgAAApwAAAKjAAkABgAA////
  319. +gAGAAACowAAAqoABgAGAAAAAP/6AAUAAgKqAAACrwAIAAYAAP////cABQAAAq8AAAK1AAkABgAA
  320. ////9wAFAAACtQAAArsACQAGAAD////3AAUAAAK7AAACwQAJAAYAAP////gABQAAAsEAAALHAAgA
  321. BgAAAAD/9wAEAAACxwAAAssACQAGAAAAAP/3AAQAAALLAAACzwAJAAYAAAAA//cABAAAAs8AAALT
  322. AAkABgAAAAD/+AAEAAAC0wAAAtcACAAGAAD////6AAUAAALXAAAC3QAGAAYAAP////cABgAAAt0A
  323. AALkAAkABgAAAAD/9wAFAAAC5AAAAukACQAGAAAAAP/3AAUAAALpAAAC7gAJAAYAAAAA//cABQAA
  324. Au4AAALzAAkABgAAAAD/9wAFAAAC8wAAAvgACQAGAAAAAP/4AAUAAAL4AAAC/QAIAAYAAAAA//oA
  325. Bf//Av0AAAMCAAUABgAA////+gAGAAADAgAAAwkABgAGAAD////3AAYAAAMJAAADEAAJAAYAAP//
  326. //cABgAAAxAAAAMXAAkABgAA////9wAGAAADFwAAAx4ACQAGAAD////4AAYAAAAAAAoABwASAAYA
  327. AP////cABgAAAAcACgAOABMABgAA////+gAFAAAADgAKABQAEAAGAAD////6AAYAAAAUAAoAGwAQ
  328. AAYAAAAA//gABgAAABsACgAhABIABgAAAAD/+AAGAAAAIQAKACcAEgAGAAAAAP/4AAYAAAAnAAoA
  329. LQASAAYAAAAA//gABgAAAC0ACgAzABIABgAAAAD/+QAGAAAAMwAKADkAEQAGAAAAAP/3AAYAAAA5
  330. AAoAPwATAAYAAP////sABQAAAD8ACgBFAA8ABgAAAAD/+wAFAAIARQAKAEoAEQAGAAAAAP/4AAUA
  331. AABKAAoATwASAAYAAAAA//gABQAAAE8ACgBUABIABgAAAAD/+AAFAAAAVAAKAFkAEgAGAAAAAP/5
  332. AAUAAABZAAoAXgARAAYAAAAA//gABgAAAF4ACgBkABIABgAAAAD/+AAGAAAAZAAKAGoAEgAGAAAA
  333. AP/4AAYAAABqAAoAcAASAAYAAAAA//kABgAAAHAACgB2ABEABgAAAAD/+AAFAAAAdgAKAHsAEgAG
  334. AAD////4AAYAAAB7AAoAggASAAYAAAAA//gABQAAAIIACgCHABIABgAAAAD/+AAFAAAAhwAKAIwA
  335. EgAGAAAAAP/4AAUAAACMAAoAkQASAAYAAAAA//gABQAAAJEACgCWABIABgAAAAD/+QAFAAAAlgAK
  336. AJsAEQAGAAAAAP/6AAX//wCbAAoAoAAPAAYAAAAA//oABQABAKAACgClABEABgAA////+AAGAAAA
  337. pQAKAKwAEgAGAAD////4AAYAAACsAAoAswASAAYAAP////gABgAAALMACgC6ABIABgAA////+QAG
  338. AAAAugAKAMEAEQAGAAD////4AAYAAgDBAAoAyAAUAAYAAP////kABQACAMgACgDOABMABgAA////
  339. +QAGAAIAzgAKANUAEw==
  340. ''')), Image.open(StringIO(base64.decodestring('''
  341. iVBORw0KGgoAAAANSUhEUgAAAx4AAAAUAQAAAAArMtZoAAAEwElEQVR4nABlAJr/AHVE4czCI/4u
  342. Mc4b7vuds/xzjz5/3/7u/n9vMe7vnfH/9++vPn/xyf5zhxzjt8GHw8+2d83u8x27199/nxuQ6Od9
  343. M43/5z2I+9n9ZtmDBwMQECDRQw/eQIQohJXxpBCNVE6QCCAAAAD//wBlAJr/AgALyj1t/wINwq0g
  344. LeNZUworuN1cjTPIzrTX6ofHWeo3v336qPzfEwRmBnHTtf95/fglZK5N0PDgfRTslpGBvz7LFc4F
  345. IUXBWQGjQ5MGCx34EDFPwXiY4YbYxavpnhHFrk14CDAAAAD//wBlAJr/AgKqRooH2gAgPeggvUAA
  346. Bu2WfgPoAwzRAABAAAAAAACQgLz/3Uv4Gv+gX7BJgDeeGP6AAAD1NMDzKHD7ANWr3loYbxsAD791
  347. NAADfcoIDyP44K/jv4Y63/Z+t98Ovt+ub4T48LAAAAD//wBlAJr/AuplMlADJAAAAGuAphWpqhMx
  348. in0A/fRvAYBABPgBwBUgABBQ/sYAyv9g0bCHgOLoGAAAAAAAREAAwI7nr0ArYpow7aX8//9LaP/9
  349. SjdavWA8ePHeBIKB//81/83ndznOaXx379wAAAD//wBlAJr/AqDxW+D3AABAAbUh/QMnbQag/gAY
  350. AYDAAACgtgD/gOqAAAB5IA/8AAAk+n9w0AAA8AAAmFRJuPo27ciC0cD5oeW4E7KA/wD3ECMAn2tt
  351. y8PgwH8AfAxFzC0JzeAMtratAsC/ffwAAAD//wBlAJr/BGKAyCAA4AAAAvgeYTAwHd1kmQF5chkG
  352. ABoMIHcL5xVpTfQbUqzlAAAErwAQBgAAEOClA5D9il08AEh/tUzdCBsXkbgACED+woQg8Si9VeqY
  353. lODCn7lmF6NhnAEYgAAA/NMIAAAAAAD//2JgjLZgVGBg5Pv/Tvpc8hwGBjYGJADjHDrAwPzAjv/H
  354. /Wf3PzCwtzcwHmBgYGcwbZz8wHaCAQMDOwMDQ8MCBgYOC3W7mp+f0w+wHOYxO3OG+e376hsMZjk3
  355. AAAAAP//YmCMY2A4wMAIN5e5gQETPD6AZisDAwMDgzSDAAPjByiHcQMDAwMDg1nOze1lByRu5/47
  356. c4859311AYNZzg0AAAAA//9iYGDBYihOIIMuwIjGL39/fwffA8b//xv/P2BPtzzHwCBjUQAAAAD/
  357. /yLFBrIBAAAA//9i1HhcwdhizX7u8NZNzyLbvT97bfrMf/QHI8evOwcSqGUJAAAA//9iYBB81iSw
  358. pEE170Qrg5MIYydHqwdDQRMrAwcVrQAAAAD//2J4x7j9AAMDn8Q/BgYLBoaiAwwMjPdvMDBYM1Tv
  359. oJodAAAAAP//Yqo/83+dxePWlxl3npsel9lvLfPcqlE9725C+acfVLMEAAAA//9i+s9gwCoaaGMR
  360. evta/58PTEWzr21hufPjA8N+qlnBwAAAAAD//2JiWLci5v1+HmFXDqcnULE/MxgYGBj+f6CaJQAA
  361. AAD//2Ji2FrkY3iYpYC5qDeGgeEMAwPDvwQBBoYvcTwOVLMEAAAA//9isDBgkP///0EOg9z35v//
  362. Gc/eeW7BwPj5+QGZhANUswMAAAD//2JgqGBgYGBgqEMXlvhMPUsAAAAA//8iYDd1AAAAAP//AwDR
  363. w7IkEbzhVQAAAABJRU5ErkJggg==
  364. '''))))
  365.     return f
  366.  
  367. if __name__ == "__main__":
  368.     # create font data chunk for embedding
  369.     import base64, os, sys
  370.     font = "../Images/courB08"
  371.     print "    f._load_pilfont_data("
  372.     print "         # %s" % os.path.basename(font)
  373.     print "         StringIO(base64.decodestring('''"
  374.     base64.encode(open(font + ".pil", "rb"), sys.stdout)
  375.     print "''')), Image.open(StringIO(base64.decodestring('''"
  376.     base64.encode(open(font + ".pbm", "rb"), sys.stdout)
  377.     print "'''))))"
  378.